<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生成绩 - 教学管理系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        .container {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 250px;
            background-color: #2c3e50;
            color: white;
            padding-top: 20px;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid #3c5166;
        }

        .sidebar-header h2 {
            margin: 0;
            font-size: 20px;
        }

        .user-info {
            margin-top: 10px;
            font-size: 14px;
            opacity: 0.8;
        }

        .menu {
            list-style: none;
            padding: 0;
            margin: 20px 0;
        }

        .menu li {
            padding: 12px 20px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .menu li:hover {
            background-color: #3c5166;
        }

        .menu li.active {
            background-color: #3498db;
        }

        .menu a {
            color: white;
            text-decoration: none;
            display: block;
        }

        .content {
            flex: 1;
            padding: 20px;
        }

        .content-header {
            margin-bottom: 30px;
        }

        .content-header h1 {
            margin: 0;
            color: #333;
            font-size: 24px;
        }

        .breadcrumb {
            display: flex;
            list-style: none;
            padding: 0;
            margin: 0 0 10px 0;
        }

        .breadcrumb li {
            font-size: 14px;
            color: #777;
        }

        .breadcrumb li:not(:last-child)::after {
            content: '>';
            margin: 0 8px;
        }

        .breadcrumb a {
            color: #3498db;
            text-decoration: none;
        }

        .breadcrumb a:hover {
            text-decoration: underline;
        }

        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            margin-bottom: 20px;
        }

        .card-header {
            background-color: #3498db;
            color: white;
            padding: 15px 20px;
        }

        .card-header h2 {
            margin: 0;
            font-size: 20px;
        }

        .card-body {
            padding: 20px;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }

        .info-item {
            margin-bottom: 10px;
        }

        .info-label {
            color: #888;
            font-size: 12px;
            margin-bottom: 4px;
        }

        .info-value {
            color: #333;
            font-size: 16px;
            font-weight: 500;
        }

        .section-title {
            font-size: 18px;
            color: #333;
            margin: 20px 0 15px 0;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        .grade-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        .grade-table th,
        .grade-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        .grade-table th {
            background-color: #f8f9fa;
            color: #333;
            font-weight: 600;
        }

        .grade-input {
            width: 60px;
            padding: 6px 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
            font-size: 14px;
        }

        .btn {
            display: inline-block;
            padding: 8px 16px;
            background-color: #3498db;
            color: white;
            border-radius: 4px;
            border: none;
            text-decoration: none;
            font-size: 14px;
            cursor: pointer;
            margin-right: 10px;
        }

        .btn:hover {
            background-color: #2980b9;
        }

        .btn-success {
            background-color: #27ae60;
        }

        .btn-success:hover {
            background-color: #219955;
        }

        .btn-warning {
            background-color: #f39c12;
        }

        .btn-warning:hover {
            background-color: #e67e22;
        }

        .btn-danger {
            background-color: #e74c3c;
        }

        .btn-danger:hover {
            background-color: #c0392b;
        }

        .alert {
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            display: none;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .action-buttons {
            display: flex;
            justify-content: flex-start;
            margin-top: 20px;
        }

        .logout {
            margin-top: auto;
            padding: 20px;
        }

        .logout a {
            color: #e74c3c;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>教学管理系统</h2>
                <div class="user-info" th:if="${session.user}">
                    <span th:text="${session.user.username}">teacher1</span> - 教师
                </div>
            </div>
            <ul class="menu">
                <li><a href="/teacher/dashboard">我的概览</a></li>
                <li><a href="/teacher/courses">我的课程</a></li>
                <li><a href="/teacher/students">我的学生</a></li>
                <li class="active"><a href="/teacher/grades">成绩管理</a></li>
                <li><a href="/teacher/profile">个人信息</a></li>
            </ul>
            <div class="logout">
                <a href="/logout">退出登录</a>
            </div>
        </div>

        <div class="content">
            <ul class="breadcrumb">
                <li><a href="/teacher/dashboard">我的概览</a></li>
                <li><a href="/teacher/courses">我的课程</a></li>
                <li><a th:href="@{'/teacher/course/' + ${course.courseId}}" th:text="${course.name}">计算机导论</a></li>
                <li><a th:href="@{'/teacher/student/' + ${student.studentId}}" th:text="${student.name}">张三</a></li>
                <li>成绩管理</li>
            </ul>

            <div class="content-header">
                <h1>学生成绩管理</h1>
            </div>

            <div class="alert alert-success" id="successAlert">成绩保存成功！</div>
            <div class="alert alert-danger" id="errorAlert">成绩保存失败，请重试！</div>

            <div class="card">
                <div class="card-header">
                    <h2>课程与学生信息</h2>
                </div>
                <div class="card-body">
                    <div class="info-grid">
                        <div class="info-item">
                            <div class="info-label">课程编号</div>
                            <div class="info-value" th:text="${course.courseId}">CS101</div>
                        </div>
                        <div class="info-item">
                            <div class="info-label">课程名称</div>
                            <div class="info-value" th:text="${course.name}">计算机导论</div>
                        </div>
                        <div class="info-item">
                            <div class="info-label">学分</div>
                            <div class="info-value" th:text="${course.credit}">4</div>
                        </div>
                        <div class="info-item">
                            <div class="info-label">开课学期</div>
                            <div class="info-value" th:text="${course.semester}">大一上</div>
                        </div>
                    </div>

                    <div class="info-grid">
                        <div class="info-item">
                            <div class="info-label">学生学号</div>
                            <div class="info-value" th:text="${student.studentId}">20230001</div>
                        </div>
                        <div class="info-item">
                            <div class="info-label">学生姓名</div>
                            <div class="info-value" th:text="${student.name}">张三</div>
                        </div>
                        <div class="info-item">
                            <div class="info-label">专业班级</div>
                            <div class="info-value" th:text="${student.className}">计算机1班</div>
                        </div>
                        <div class="info-item">
                            <div class="info-label">选课状态</div>
                            <div class="info-value" th:text="${selection.status}">正在修</div>
                        </div>
                    </div>
                </div>
            </div>

            <h3 class="section-title">成绩录入</h3>

            <div class="card">
                <div class="card-body">
                    <form id="gradeForm">
                        <input type="hidden" id="selectionId" name="selectionId" th:value="${selection.selectionId}">
                        <table class="grade-table">
                            <thead>
                                <tr>
                                    <th>评分项目</th>
                                    <th>权重</th>
                                    <th>满分</th>
                                    <th>得分</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="component, status : ${selection.gradeComponents}">
                                    <td th:text="${component.name}">期末考试</td>
                                    <td th:text="${component.weight + '%'}">70%</td>
                                    <td th:text="${component.maxScore}">100</td>
                                    <td>
                                        <input type="number" class="grade-input" th:id="'score-' + ${status.index}"
                                            th:value="${component.score}" th:attr="data-weight=${component.weight}"
                                            min="0" th:max="${component.maxScore}" step="0.5"
                                            onchange="updateTotalScore()">
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>总评</strong></td>
                                    <td>100%</td>
                                    <td>100</td>
                                    <td>
                                        <input type="number" class="grade-input" id="totalScore"
                                            th:value="${selection.score}" readonly>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                        <div class="action-buttons">
                            <button type="button" class="btn btn-success" onclick="saveGrade()">保存成绩</button>
                            <a th:href="@{'/teacher/student/' + ${student.studentId}}" class="btn">返回</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            console.log('学生成绩管理页面加载完成');
        });

        // 计算总评分
        function updateTotalScore() {
            const components = document.querySelectorAll('[id^="score-"]');
            let weightedSum = 0;
            let totalWeight = 0;

            components.forEach(component => {
                const score = parseFloat(component.value) || 0;
                const weight = parseFloat(component.getAttribute('data-weight')) || 0;

                weightedSum += (score * weight);
                totalWeight += weight;
            });

            // 计算加权平均分
            const totalScore = totalWeight > 0 ? (weightedSum / totalWeight).toFixed(1) : 0;
            document.getElementById('totalScore').value = totalScore;
        }

        // 保存成绩
        function saveGrade() {
            const selectionId = document.getElementById('selectionId').value;
            const totalScore = document.getElementById('totalScore').value;

            // 显示保存中状态
            const saveButton = document.querySelector('.btn-success');
            const originalText = saveButton.textContent;
            saveButton.textContent = '保存中...';
            saveButton.disabled = true;

            // 发送请求到后端保存成绩
            fetch('/teacher/grade/save', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    gradeId: selectionId,
                    score: totalScore
                }),
                credentials: 'include'
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('保存失败');
                    }
                    return response.json();
                })
                .then(data => {
                    console.log('成绩保存成功:', data);

                    // 显示成功消息
                    const successAlert = document.getElementById('successAlert');
                    successAlert.style.display = 'block';

                    // 3秒后隐藏成功消息
                    setTimeout(() => {
                        successAlert.style.display = 'none';
                    }, 3000);

                    // 恢复按钮状态
                    saveButton.textContent = originalText;
                    saveButton.disabled = false;
                })
                .catch(error => {
                    console.error('保存成绩失败:', error);

                    // 显示错误消息
                    const errorAlert = document.getElementById('errorAlert');
                    errorAlert.style.display = 'block';

                    // 3秒后隐藏错误消息
                    setTimeout(() => {
                        errorAlert.style.display = 'none';
                    }, 3000);

                    // 恢复按钮状态
                    saveButton.textContent = originalText;
                    saveButton.disabled = false;
                });
        }
    </script>
</body>

</html>